jQuery(function(){
	///新建投诉与建议
	jQuery('.addBtn').on('click',function(){
		window.location = window.rootPath+"/ui/aftersalecomplain/add";
	})
});
jQuery(function () {

	////获得投诉建议信息列表
	var serverData = {};
	serverData.key='';
	var serverTable;
	jQuery(document).ready(function () {
		serverTable = jQuery('#accessTable').DataTable({
			"serverSide": true,
			"ordering": false,
			"ajax": {
				"dataType": "json",
				"type": "get",
				"cache": false,
				"url":window.rootPath+ '/ui/aftersalecomplain/getpage',
				"data": function (d) {
					return $.extend({}, d, serverData);
				}
			},
			columns: [
				{
					data: null
				},
				{
					data: null,
					defaultContent: "",
					render: function (data, type, full, meta) {
						var id=data.id
						var operate ='<input type="checkbox" class="checkchild">';
						return operate;
					}
				},
				{
					data: 'customerName',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 5) ? '<font title="'+data+'">'+data.substring(0,5)+'...'+'</font>' : data;
						}
					}
				},
				{
					data: 'contractID'
				},
				{
					data: 'address',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 8) ? '<font title="'+data+'">'+data.substring(0,8)+'...'+'</font>' : data;
						}
					}
				},
				{
					data: 'phone'
				},
				{
					data: 'fax'
				},
				{
					data: 'contact'
				},
				{
					data: 'createTime',
			render: function (data, type, full, meta) {
				if (data)
					return moment(data).format('YYYY-MM-DD');
				else
					return '';
			}
				},
				{
					data: 'memo',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 12) ? '<font title="'+data+'">'+data.substring(0,12)+'...'+'</font>' : data;
						}
					}
				},
				{
					data:'attachCount',
					 render: function (data, type, full, meta) {
	                 	var dOut;
	                 	if(data==null){
	                 		dOut='<td class="center" ><span class="badge label-pill label-primary">'+0+'</span></td>'
	                 	}else{
	                 		dOut='<td class="center" ><span class="badge label-pill label-primary">'+data+'</span></td>'
	                 	}
	                 	return dOut;
	                 }
				},
				{
					data: 'user.name'
				}

			],
			"drawCallback": function (oSettings) {
				var k = 0;
				var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

				for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
					$('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
					k++;
				}
			}
		});
// 下载附件
		jQuery(".downloadBtn").on('click', function() {
			var selects=getSelect("#accessTable",1);
			var newsId=selects[0].id;
			filedown(newsId, '投诉建议附件',1);
		});
	});

	/////删除/////
	jQuery('.delBtn').on('click', function () {
		var selects=getSelect("#accessTable",1);
		var url=window.rootPath+"/ui/aftersalecomplain/delComplain";
		delSelect(selects,url,serverTable)
	});

	///////列表中的全选
	$("#checkAll").click(function(){
		var check = $(this).prop("checked");
		$(".checkchild").prop("checked", check);
	});

	/////新增投诉建议信息
	jQuery(".addBtn").on("click",function(){
		window.location = window.rootPath+"/ui/aftersalecomplain/add";
	});

	////编辑
	jQuery('.editBtn').on('click', function () {
		var selects=getSelect("#accessTable",1);
		if(selects.length>1){
			$.springLemonDialog({
				content: '只能编辑一条记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		}
		else if(selects.length==0){
			$.springLemonDialog({
				content: '请选择要编辑的记录',// 确认文本
				clickButton: function (sender, modal, index) {// 按钮事件处理
					$(this).closeDialog(modal);
				}
			});
		}else if(selects.length==1){
			var complainId=selects[0].id;
			window.location = window.rootPath+"/ui/aftersalecomplain/edit"+"?complainId="+complainId;
		}
	});

	//查询
	jQuery('.searchBtn').on('click',function(){
		serverData.key=jQuery('#keyword').val();
		serverTable.draw();
	})
	//撤销查询
	jQuery('.cancelBtn').on("click",function()
	{
		jQuery('#keyword').val('');
		serverData.key ='';
		serverTable.draw();
	});


});
